﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using System.Configuration;
using System.Data;
using TGIApp.Model;

namespace TGIApp.Web.Controllers
{
    public class CustomerController : Controller
    {
        //
        // GET: /Customer/

        public ActionResult Index()
        {
            //var context = new TicketsModelContainer(new Uri(ConfigurationManager.AppSettings["DataServiceUri"]));
            //var customers = context.AllUsers.Where(user => user is Customer);
            //return View(customers);
            return View();
        }

        //
        // GET: /Customer/Details/5

        public ActionResult Details(int id)
        {
            //var context = new TicketsModelContainer(new Uri(ConfigurationManager.AppSettings["DataServiceUri"]));
            //var customer = (Customer)context.AllUsers.Expand("Address").Where(user => user is Customer  && user.Id == id).FirstOrDefault();

            //return View(customer);
            return View();
        }

        //
        // GET: /Customer/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Customer/Create

        [HttpPost]
        public ActionResult Create(User customer)
        {

            if (ModelState.IsValid)
            {
               // var context = new TicketsModelContainer(new Uri(ConfigurationManager.AppSettings["DataServiceUri"]));
                
               // //context.MergeOption = System.Data.Services.Client.MergeOption.AppendOnly;
               // var role = context.Roles.Where(r => r.Name.ToLower() == "customer").FirstOrDefault();
               //// var address = new Address() { Line1 = "Line1", Line2 = "Line2", PostalCode = "1111", State = "State", City = "City", Country = "Country" };
               // var address = customer.Address;
               // context.AddToAddresses(address);
                

               //// user.Address = address;
               // customer.Role = role;
                
               // context.AddToAllUsers(customer);
                
               // context.SetLink (customer, "Role", role); 
               // context.SetLink(customer, "Address", address); 

               // context.SaveChanges();
                return View();
            }

            return RedirectToAction("Index");
        }

        //
        // GET: /Customer/Edit/5

        public ActionResult Edit(int id)
        {
            //var context = new TicketsModelContainer(new Uri(ConfigurationManager.AppSettings["DataServiceUri"]));
            //var customer = (Customer)context.AllUsers.Expand("Address").Where(user => user is Customer  && user.Id == id).FirstOrDefault();
            //return View(customer);
            return View();
        }

        //
        // POST: /Customer/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, User customer)
        {

            //if (ModelState.IsValid)
            //{
            //    var context = new TicketsModelContainer(new Uri(ConfigurationManager.AppSettings["DataServiceUri"]));
            //    context.AttachTo("AllUsers", customer);
            //    context.AttachTo("Addresses", customer.Address);
            //    context.UpdateObject(customer.Address);
            //    context.UpdateObject(customer);
                
            //    context.SaveChanges( System.Data.Services.Client.SaveChangesOptions.Batch);

            //}

            //return RedirectToAction("Index");
            return View();
        }

        //
        // GET: /Customer/Delete/5

        public ActionResult Delete(int id)
        {
            //var context = new TicketsModelContainer(new Uri(ConfigurationManager.AppSettings["DataServiceUri"]));
            //var customer = (Customer)context.AllUsers.Expand("Address").Where(user => user is Customer  && user.Id == id).FirstOrDefault();
            //return View(customer);
            return View();
        }

        //
        // POST: /Customer/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, User customerToBeDeleted)
        {

            //var context = new TicketsModelContainer(new Uri(ConfigurationManager.AppSettings["DataServiceUri"]));
            //context.AttachTo("AllUsers", customerToBeDeleted);
            //context.AttachTo("Addresses", customerToBeDeleted.Address);
            //context.DeleteObject(customerToBeDeleted);
            //context.DeleteObject(customerToBeDeleted.Address);
            //context.SaveChanges(System.Data.Services.Client.SaveChangesOptions.Batch);
            //return RedirectToAction("Index");
            return View();
           
        }
    }
}
